package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：乔博宇
 * @date ：Created in 2019/7/28 13:25
 * @description ：
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("menu")
public class MenuController {

 @Reference
 private MenuService menuService;

 @RequestMapping("findAll")
 public Result findAll(){
/**
 *   private Set<Role> roles = new HashSet<Role>(0);//角色集合
 *     private List<Menu> children = new ArrayList<>();//子菜单集合
 *     private Integer parentMenuId;//父菜单id
 */
  try {

   //从授权框架上下文获取授权对象，再从授权对象获取授权框架用户对象User,最后获取用户名
   Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   User user = (User)authentication.getPrincipal();
   String username = user.getUsername();
   System.out.println(username);

   List<Map<String , Object >> menuList = menuService.findAll(username);

   for (Map<String, Object> stringObjectMap : menuList) {

    Integer id = (Integer) stringObjectMap.get("id");
    List<Map<String,String>> childrens =  menuService.findByPar(id);

      System.out.println(childrens+"------------------------");
    stringObjectMap.put("children",childrens);
   }

   return  new Result( true,MessageConst.ACTION_SUCCESS,menuList);
  } catch (Exception e) {
   e.printStackTrace();
   return  new Result( true,MessageConst.ACTION_FAIL);
  }
 }


 //=========================
//权限校验
 /*@RequestMapping("/add")
 public Result add(@RequestBody Menu menu,Integer[] roleIds){
  try {
   menuService.add(menu,roleIds);
   return new Result(true,MessageConst.ACTION_SUCCESS);
  }catch (Exception e){
   e.printStackTrace();
   return new Result(false,MessageConst.ACTION_FAIL);
  }

 }

 @RequestMapping("/findPage")
 public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {
  try {
   System.err.println(">>>>>>>11111111111111111111>>>>>>>>>>>>>>>>>>>>>>"+queryPageBean.toString());
   return menuService.pageQuery(queryPageBean);
  }catch (Exception e){
   e.printStackTrace();
  }
  return new PageResult(0L,new ArrayList());
 }


 @RequestMapping("/delete")
 public Result delete(Integer id){
  try {
   menuService.deleteById(id);
   return  new Result(true,MessageConst.ACTION_SUCCESS);
  }catch (Exception e){
   e.printStackTrace();
   return new Result(false,e.getMessage());
  }
 }

 @RequestMapping("/findById")
 public Result findById(Integer id){
  try {
   Menu byId = menuService.findById(id);
   return new Result(true,MessageConst.ACTION_SUCCESS,byId);
  }catch (Exception e){
   e.printStackTrace();
   return  new Result(false,MessageConst.ACTION_FAIL);
  }
 }
 @RequestMapping("/findroleIdsBymenuId")
 public Result findroleIdsBymenuId(Integer id) {
  try {
   List<Integer> list = menuService.findroleIdsBymenuId(id);
   return new Result(true, MessageConst.ACTION_SUCCESS, list);
  } catch (Exception e) {
   e.printStackTrace();
   return new Result(true, MessageConst.ACTION_FAIL);
  }
 }


 @RequestMapping("/edit")
 public Result edit(@RequestBody Menu menu, Integer[] roleIds){
  try {
   menuService.edit(menu,roleIds);
   return new Result(true,MessageConst.ACTION_SUCCESS);

  }catch (Exception e){
   e.printStackTrace();
   return new Result(false,MessageConst.ACTION_FAIL);

  }

 }*/




}
